package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.CourseCollect;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.service.CourseCollectService;
import com.wxxymaker.edu.utils.TokenUtils;
import io.github.biezhi.anima.page.Page;

/**
 * @Author: zhanghuan
 * @Date: 2018/9/15 下午4:09
 * @DESCRIBE: 课程收藏相关操作接口
 */

@StudentRoles
@Path(value = "/course", restful = true)
public class CourseCollectHandlerApi {

    @Inject
    private CourseCollectService courseCollectService;

    /**
     * 课程收藏
     *
     * @param kid 课程id
     */
    //能重复收藏？？？应该需要通过前端控制收藏按钮吧
    @PostRoute("/:kid/collect")  // /course/2/info
    public RestResponse collect(@PathParam int kid) {
        CourseCollect courseCollect = new CourseCollect();
        courseCollect.setUserId(TokenUtils.currentUser().getId());
        courseCollect.setCourseId(kid);
        CourseCollect addCourseCollect = courseCollectService.add(courseCollect);
        if (addCourseCollect != null) {
            return RestResponse.ok(addCourseCollect);
        }
        return RestResponse.fail();
    }


    /**
     * 取消收藏课程
     *
     * @param kid 课程id
     */
    @PostRoute("/:kid/collect/cancel")
    public RestResponse cancelCollect(@PathParam int kid) {
        boolean cancel = courseCollectService.cancel(TokenUtils.currentUser().getId(), kid);
        if (cancel) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 当前用户收藏的课程
     * 指定用户收藏的课程
     * @param page 页码
     * @param size 分页大小
     * @return
     */
    @PostRoute("/page")
    public RestResponse page(@Param(defaultValue = "0") int uid,
                             @Param(defaultValue = "1") int page,
                             @Param(defaultValue = "10") int size) {
        Page<CourseCollect> courseCollectPage = null;
        if (uid == 0) {
            courseCollectPage = courseCollectService.page(TokenUtils.currentUser().getId(), page, size);
        } else {
            courseCollectPage = courseCollectService.page(uid, page, size);
        }
        if (courseCollectPage.getTotalRows() > 0) {
            return RestResponse.ok(courseCollectPage);
        }
        return RestResponse.fail();
    }

}